local skynet = require "skynet"
local filelog = require "filelog"
local tabletool = require "tabletool"
local timer = require "timer"
require "pubenum"
require "dnenum"

local seatlogic = {
    server = nil,
}

function seatlogic.init(service)
    if seatlogic.server == nil then
        seatlogic.server = service
    end
end

function seatlogic.initseat(seatobj, index)
    seatobj.index = index
    seatobj.state = DNSeatState.SEAT_STATE_NO_PLAYER
	seatobj.is_tuoguan = EBOOL.FALSE
end

function seatlogic.is_empty(seatobj)
    return (seatobj.state == DNSeatState.SEAT_STATE_NO_PLAYER)
end

function seatlogic.is_ingame(seatobj)
	if not seatobj.rid or seatobj.rid == 0 then 
		return false
	end
	if seatobj.state == DNSeatState.SEAT_STATE_WAIT_NEXT_GAME then
		return false
	end
	return true
end

function seatlogic.set_no_puttimes(seatobj)
    local server = seatlogic.server
	local table_data = server.table_data
	local allzu = 0

	local cardscontainer = table_data.eqsgame.extract_eqscards(seatobj.cards)
	if cardscontainer.gun ~= nil then
		if #cardscontainer.gun > 0 then
			allzu = allzu + #cardscontainer.gun
		end
	end
	for index, cards in ipairs(seatobj.uncards) do
		local tempcards = tabletool.deepcopy(cards)
        local cardhelper = table_data.eqsgame.CreateCardsHelper(tempcards)
        cardhelper:GetCardsType(cardhelper)
        if cardhelper.m_eCardType == ECardType.EQS_CARD_TYPE_GUN then
        	allzu = allzu + 1
        end
	end
	seatobj.gun_nums = allzu
end

function seatlogic.dealcards(seatobj)
    local server = seatlogic.server
	local table_data = server.table_data
    local noticemsg = {
		rid = seatobj.rid,
		seat_index = seatobj.index,
		cards = seatobj.cards
	}
	if table_data.conf.table_type == DNTableType.TABLE_TYPE_TRAIN then
		if seatobj.is_force_quit == 0 then
    		server:send_notice_to_players({seatobj.rid}, "dealcardsN", noticemsg, nil, nil)
		end
	else
		if table_data.conf.match_type ~= 0 then
			if seatobj.is_matching == false then
				server:send_notice_to_players({seatobj.rid}, "dealcardsN", noticemsg, nil, nil)
			end
		else
			server:send_notice_to_players({seatobj.rid}, "dealcardsN", noticemsg, nil, nil)
		end
	end
end

function seatlogic.onegamestart_initseat(seat)
    seat.state = DNSeatState.SEAT_STATE_PLAYING
    seat.continue_games = seat.continue_games + 1
	seat.ready_to_time = 0
	if seat.ready_timer_id > 0 then
		timer.cleartimer(seat.ready_timer_id)
		seat.ready_timer_id = -1
	end
end

function seatlogic.resetstate(seatobj)
	if seatobj == nil then return end
	local server = seatlogic.server
	local table_data = server.table_data
	seatobj.state = DNSeatState.SEAT_STATE_WAIT_READY
	seatobj.timeout_count = 0							---玩家操作超时次数
	seatobj.timer_id = -1
	seatobj.win = 0										---表示玩家胜利还是失败
	seatobj.is_tuoguan = DNBOOL.FALSE
	seatobj.ready_timer_id = -1 						---准备倒计时定时器
	seatobj.ready_to_time = 0   						---准备到期时间
	seatobj.cards = {}   								---玩家手牌
	seatobj.balance_score = 0	 						----最终结算分数
	seatobj.tuoguan_punish = 0
	seatobj.followhandsnum = 0
	seatobj.holdvalue = 0 		 						----玩家货币持有量
	seatobj.dealnum = 0
	seatobj.cancel_timerid = -1							----解散牌桌定时器
	seatobj.final_cards = nil
	seatobj.action_type = 0
	seatobj.action_to_time = 0							----操作超时时间
	seatobj.iszj = DNPlayerIDType.DN_PLAYER_ID_UNKNOW  	----记录玩家身份(是否是庄家)
	if table_data.conf.table_type ~= DNTableType.TABLE_TYPE_FANGKA then
    	seatobj.score = 0
    end
	seatobj.final_cards = nil	
	seatobj.is_disconnected = 0	 						----玩家是否断线
	if table_data.conf.table_type == DNTableType.TABLE_TYPE_TRAIN then
		if seatobj.is_force_quit == 1 then end
	else
		seatobj.is_force_quit = 0	 						----玩家是否强退
	end 
	seatobj.holdvalue = 0 		 						----玩家货币持有量
	seatobj.dealnum = 0 		 						----玩家中间结算量
	seatobj.qiangzhuang_times = 0						----玩家抢庄下的倍数
	seatobj.xiazhu_times = 0							----玩家下注倍数
	seatobj.card_type = DNCardType.CARD_TYPE_UNKNOWN
	seatobj.is_matching = false
	seatobj.is_delay_leave = false
end

function seatlogic.clear_seat(seat)
	if seat == nil then return end
	seat.rid = 0
	seat.state = DNSeatState.SEAT_STATE_NO_PLAYER
	seat.gatenode_id = ""
	seat.agent_address = -1
	seat.playerinfo.rolename = ""
	seat.playerinfo.logo=""
	seat.playerinfo.sex = 0
    seat.playerinfo.coins = 0
    seat.playerinfo.level = 0
    seat.playerinfo.diamond = 0
	seat.playerinfo.upvoted_num = 0
	seat.playerinfo.downvoted_num = 0
	seat.playerinfo.ip_address = ""
	seat.playerinfo.gps_addr = ""
	seat.playerinfo.logoframe = 0
	
	seat.timeout_count = 0							---玩家操作超时次数
	seat.timer_id = -1
	seat.win = 0										---表示玩家胜利还是失败
	seat.is_tuoguan = DNBOOL.FALSE
	seat.ready_to_time = 0   						---准备到期时间
	seat.cards = {}   								---玩家手牌
	seat.balance_score = 0	 						----最终结算分数
	seat.tuoguan_punish = 0
	seat.holdvalue = 0 		 						----玩家货币持有量
	seat.dealnum = 0
	seat.cancel_timerid = -1							----解散牌桌定时器
	seat.final_cards = nil
	seat.action_type = 0
	seat.action_to_time = 0							----操作超时时间
	seat.iszj = DNPlayerIDType.DN_PLAYER_ID_UNKNOW  	----记录玩家身份(是否是庄家)
    seat.score = 0
	seat.diamond = 0
	seat.is_disconnected = 0	 						----玩家是否断线
	seat.is_force_quit = 0	 						----玩家是否强退 
	seat.holdvalue = 0 		 						----玩家货币持有量
	seat.dealnum = 0 		 						----玩家中间结算量
	seat.qiangzhuang_times = 0						----玩家抢庄下的倍数
	seat.xiazhu_times = 0							----玩家下注倍数
	seat.card_type = DNCardType.CARD_TYPE_UNKNOWN
	seat.train_score = 0
	seat.is_robot = false		--是否是机器人
	seat.is_mirror = false
	seat.is_matching = false
	seat.balance_total = 0
	seat.continue_games = 0
	seat.is_delay_leave = false

	if seat.ready_timer_id > 0 then
		timer.cleartimer(seat.ready_timer_id)
	end
	seat.ready_timer_id = -1
end

function seatlogic.cal_can_qiangzhuang_times(seat, times)
	local server = seatlogic.server
	local table_data = server.table_data
	local is_can_qiang = true
	if table_data.conf.table_type == DNTableType.TABLE_TYPE_TRAIN or table_data.conf.table_type == DNTableType.TABLE_TYPE_FANGKA then return is_can_qiang end
	local qiang_times_must = {}
	local in_game_num = 0
	for index, seatobj in ipairs(table_data.seats) do
		if seatobj.rid ~= 0 and seatobj.state ~= DNSeatState.SEAT_STATE_WAIT_NEXT_GAME then
			in_game_num = in_game_num + 1
		end
	end
	----filelog.sys_error("-------in_game_num------", in_game_num)
	for index, xia_times in ipairs(DNQiangzhuangTimes[table_data.conf.xia_zhu_type]) do
		if qiang_times_must[xia_times] == nil then
			qiang_times_must[xia_times] = 0
		end
		qiang_times_must[xia_times] = 3 * (in_game_num -1) * xia_times * table_data.conf.score_diamond 
	end
	--filelog.sys_error("-----cal_can_qiangzhuang_times---", times, qiang_times_must)
	if qiang_times_must[times] == nil then
		is_can_qiang = false
	else
		--filelog.sys_error("------qiang_times_must[times]--",qiang_times_must[times], "-----seat.diamond------",seat.diamond)
		if table_data.conf.table_type == DNTableType.TABLE_TYPE_ZUANSI or table_data.conf.table_type == DNTableType.TABLE_TYPE_ZJZUANSI then
			if seat.diamond < qiang_times_must[times] then
				is_can_qiang = false
			end
		elseif table_data.conf.table_type == DNTableType.TABLE_TYPE_COIN then
			if seat.coin < qiang_times_must[times] then
				is_can_qiang = false
			end
		end
	end
	return is_can_qiang
end

return seatlogic